package com.acce.gxsc.page.outCar.batchList;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.acce.gxsc.R;
import com.acce.gxsc.common.Tools;
import com.acce.gxsc.model.netBean.NetBatchBean;
import com.acce.gxsc.model.netBean.NetInBatchListBean;
import com.acce.gxsc.page.outCar.batchDetail.BatchDetailActivity;
import com.acce.sdk.base.mvp.BaseMvpActivity;
import com.acce.sdk.utils.StringUtils;
import com.acce.sdk.utils.ToastUtils;
import com.acce.sdk.widget.EditWithTitle;
import com.acce.sdk.widget.adapterrv.ViewHolder;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by huxley on 2017/5/26.
 */

public class ToOutBatchCarListActivity extends BaseMvpActivity<ToOutBatchCarListView, ToOutBatchCarListPresenter> implements ToOutBatchCarListView {

    String queryId = "";
    LayoutInflater     inflater;
    NetInBatchListBean mInBatchListBean;
    boolean isHas = false;

    @Bind(R.id.tv_car_no)
    TextView     mTvCarNo;
    @Bind(R.id.tv_type)
    TextView     mTvType;
    @Bind(R.id.tv_incar_id)
    TextView     mTvIncarId;
    @Bind(R.id.tv_stay_time)
    TextView     mTvStayTime;
    @Bind(R.id.tv_box_no)
    TextView     mTvBoxNo;
    @Bind(R.id.tv_park_area)
    TextView     mTvParkArea;
    @Bind(R.id.tv_park_position)
    TextView     mTvParkPosition;
    @Bind(R.id.ll_detail)
    LinearLayout mLlDetail;
    @Bind(R.id.ll_content)
    LinearLayout mLlContent;
    @Bind(R.id.tv_incar_position)
    TextView     mTvIncarPosition;
    @Bind(R.id.tv_incar_name)
    TextView     mTvIncarName;
    @Bind(R.id.tv_incar_time)
    TextView     mTvIncarTime;
    @Bind(R.id.tv_supervision_state)
    TextView     mTvSupervisionState;
    @Bind(R.id.tv_supervision_name)
    TextView     mTvSupervisionName;
    @Bind(R.id.tv_supervision_time)
    TextView     mTvSupervisionTime;
    @Bind(R.id.tv_outcar_state)
    TextView     mTvOutcarState;
    @Bind(R.id.tv_outcar_name)
    TextView     mTvOutcarName;
    @Bind(R.id.tv_outcar_time)
    TextView     mTvOutcarTime;
    @Bind(R.id.tv_state)
    TextView     mTvState;
    @Bind(R.id.tv_diff_out_car)
    TextView     mTvDiffOutCar;
    @Bind(R.id.tv_out_car)
    TextView     mTvOutCar;
    @Bind(R.id.tv_company_name)
    TextView     mTvCompanyName;
    @Bind(R.id.tv_car_type)
    TextView     mTvCarType;
    @Bind(R.id.tv_registercar_position)
    TextView     mTvRegistercarPosition;
    @Bind(R.id.tv_registercar_name)
    TextView     mTvRegistercarName;
    @Bind(R.id.tv_registercar_time)
    TextView     mTvRegistercarTime;


    @Override
    protected ToOutBatchCarListPresenter createPresenter() {
        return new ToOutBatchCarListPresenter();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_to_out_car_list);
        ButterKnife.bind(this);
        inflater = LayoutInflater.from(this);

        if (getIntent().hasExtra("queryId")) {
            queryId = getIntent().getStringExtra("queryId");
        }
        mPresenter.loadInCarList(queryId);
    }

    private void setData(final NetInBatchListBean inBatchListBean) {
        Tools.setStringWithTitle(mTvBoxNo, "挂号/柜号：", Tools.getString1(inBatchListBean.incarInfo.hangNum, inBatchListBean.incarInfo.containNo));
        Tools.setStringWithTitle(mTvCarNo, "车牌号码：", inBatchListBean.incarInfo.plateNum);
        Tools.setStringWithTitle(mTvStayTime, "驻留时间：", Tools.getStayTime(inBatchListBean.incarInfo.createTime));
        Tools.setStringWithTitle(mTvType, "登记类型：", inBatchListBean.incarInfo.diliveryType);
        Tools.setStringWithTitle(mTvIncarId, "进场单号：", inBatchListBean.incarInfo.inCarId);
        Tools.setStringWithTitle(mTvParkArea, "停放区域：", inBatchListBean.incarInfo.parkArea);
        Tools.setStringWithTitle(mTvParkPosition, "车    位：", inBatchListBean.incarInfo.parkPosition);
        mTvIncarPosition.setText(Tools.getString(inBatchListBean.incarInfo.inCarGateNo));
        mTvIncarName.setText(Tools.getString(inBatchListBean.incarInfo.inCarRegistrant));
        mTvIncarTime.setText(Tools.getTime(inBatchListBean.incarInfo.createTime));
        mTvSupervisionState.setText(Tools.getSupervisionState(inBatchListBean.incarInfo.superviseOther));
        mTvSupervisionName.setText(Tools.getString(inBatchListBean.incarInfo.superviseRegistrant));
        mTvSupervisionTime.setText(Tools.getTime(inBatchListBean.incarInfo.superviseRegisterDate, inBatchListBean.incarInfo.superviseRegisterTime));
        mTvOutcarState.setText(Tools.getOutCarState(inBatchListBean.incarInfo.outCarStatus));
        mTvOutcarName.setText(Tools.getString(inBatchListBean.incarInfo.outCarGateNo));
        mTvOutcarTime.setText(Tools.getTime(inBatchListBean.incarInfo.outCarRegisterDate, inBatchListBean.incarInfo.outCarRegisterTime));


        Tools.setStringWithTitle(mTvCarType, "车辆类型：", inBatchListBean.incarInfo.carType);
        Tools.setStringWithTitle(mTvCompanyName, "运输公司：", inBatchListBean.incarInfo.underCompany);
        mTvRegistercarPosition.setText(Tools.getString(inBatchListBean.incarInfo.confirmGateName));
        mTvRegistercarName.setText(Tools.getString(inBatchListBean.incarInfo.confirmRegistrant));
        mTvRegistercarTime.setText(Tools.getTime(inBatchListBean.incarInfo.confirmRegisterDate, inBatchListBean.incarInfo.confirmRegisterTime));
        if ("1".equals(inBatchListBean.incarInfo.outCarStatus)) {
            Tools.setStringWithTitle(mTvState, "已离场");
            mTvOutCar.setEnabled(false);
            mTvOutCar.setBackgroundResource(R.color.color_e4e4e4);
            mTvDiffOutCar.setEnabled(false);
            mTvDiffOutCar.setBackgroundResource(R.color.color_e4e4e4);
        } else {
            Tools.setStringWithTitle(mTvState, "未离场");
            isHas = false;
            for (NetBatchBean batchCar : inBatchListBean.inBatchList) {
                if (StringUtils.isEmpty(batchCar.fee2Id) || StringUtils.isEmpty(batchCar.fee3Id) || "1".equals(batchCar.fee2Id) || batchCar.fee2Id.startsWith("33") || "1".equals(batchCar.fee3Id) || batchCar.fee3Id.startsWith("33")) {
                    isHas = true;
                    break;
                }
            }
            mTvOutCar.setEnabled(!isHas);
            mTvOutCar.setBackgroundResource(isHas ? R.color.color_e4e4e4 : R.color.red_c);
            mTvDiffOutCar.setEnabled(!isHas);
            mTvDiffOutCar.setBackgroundResource(isHas ? R.color.color_e4e4e4 : R.color.red_5);
        }
        mTvOutCar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (StringUtils.isEmpty(inBatchListBean.incarInfo.inCarId)) {
                    ToastUtils.show("未找到该进场单！");
                    return;
                }
                final AlertDialog.Builder builder1 = new AlertDialog.Builder(ToOutBatchCarListActivity.this);
                final AlertDialog alertDialog1 = builder1
                        .setTitle("提示")
                        .setMessage("确定离场？")
                        .setNegativeButton("取消", null)
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                mPresenter.toOutCar(inBatchListBean.incarInfo.inCarId, "");
                            }
                        }).create();
                alertDialog1.setCanceledOnTouchOutside(false);
                alertDialog1.show();
            }
        });
        mTvDiffOutCar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (StringUtils.isEmpty(inBatchListBean.incarInfo.inCarId)) {
                    ToastUtils.show("未找到该进场单！");
                    return;
                }
                LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_diff_out_car, (ViewGroup) findViewById(R.id.dialog));
                final EditWithTitle ewtCarNo = ButterKnife.findById(layout, R.id.ewt_car_no);
                final EditWithTitle ewtHangNo = ButterKnife.findById(layout, R.id.ewt_hang_no);
                final EditWithTitle ewtBoxNo = ButterKnife.findById(layout, R.id.ewt_box_no);
                final EditWithTitle ewtRemark = ButterKnife.findById(layout, R.id.ewt_remark);
                final AlertDialog.Builder builder = new AlertDialog.Builder(ToOutBatchCarListActivity.this);
                final AlertDialog alertDialog = builder
                        .setView(layout)
                        .setTitle("请填写差异信息")
                        .setNegativeButton("取消", null)
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                String carNo = ewtCarNo.getContent();
                                String hangNo = ewtHangNo.getContent();
                                String boxNo = ewtBoxNo.getContent();
                                String remark = ewtRemark.getContent();
                                if (StringUtils.isEmpty(carNo) && StringUtils.isEmpty(hangNo) && StringUtils.isEmpty(boxNo)) {
                                    ToastUtils.show("至少填写一项差异信息");
                                    return;
                                }
                                mPresenter.toOutCar(inBatchListBean.incarInfo.inCarId, Tools.getString4(carNo, hangNo, boxNo, remark));
                            }
                        }).create();
                alertDialog.setCanceledOnTouchOutside(false);
                alertDialog.show();
            }
        });
        mLlContent.removeAllViews();
        for (final NetBatchBean batchBean : inBatchListBean.inBatchList) {
            View chile = inflater.inflate(R.layout.item_out_car_1_batch, mLlContent, false);
            ViewHolder holder = new ViewHolder(ToOutBatchCarListActivity.this, chile);
            holder.setText(R.id.tv_inbatch_id, batchBean.inBatchId);
            holder.setText(R.id.tv_contact_name, batchBean.contactName);
            holder.setText(R.id.tv_goods_info, batchBean.goodsInfo);
            if (StringUtils.isEmpty(batchBean.fee2Id) || StringUtils.isEmpty(batchBean.fee3Id)) {
                holder.setText(R.id.tv_fee_1, "1#门单据");
                holder.setText(R.id.tv_fee_2, "1#门单据");
                holder.setBackgroundColor(R.id.ll_state, Tools.getColor(R.color.yellow));
                holder.setText(R.id.tv_state, "？");
                holder.setTextColor(R.id.tv_fee_1, Color.WHITE);
                holder.setTextColor(R.id.tv_fee_2, Color.WHITE);
                holder.setTextColor(R.id.tv_state, Color.WHITE);
                holder.setTextColor(R.id.tv_contact_name, Color.WHITE);
                holder.setTextColor(R.id.tv_goods_info, Color.WHITE);
            } else {
                if ("1".equals(batchBean.fee2Id) || batchBean.fee2Id.startsWith("33") || "1".equals(batchBean.fee3Id) || batchBean.fee3Id.startsWith("33")) {
                    holder.setBackgroundColor(R.id.ll_state, Tools.getColor(R.color.morning_glory));
                    holder.setText(R.id.tv_state, "×");
                    holder.setTextColor(R.id.tv_fee_1, Color.RED);
                    holder.setTextColor(R.id.tv_fee_2, Color.RED);
                    holder.setTextColor(R.id.tv_state, Color.RED);
                    holder.setTextColor(R.id.tv_contact_name, Color.RED);
                    holder.setTextColor(R.id.tv_goods_info, Color.RED);
                } else {
                    holder.setBackgroundColor(R.id.ll_state, Color.WHITE);
                    holder.setText(R.id.tv_state, "√");
                    holder.setTextColor(R.id.tv_fee_1, Tools.getColor(R.color.text_lgray));
                    holder.setTextColor(R.id.tv_fee_2, Tools.getColor(R.color.text_lgray));
                    holder.setTextColor(R.id.tv_state, Tools.getColor(R.color.text_lgray));
                    holder.setTextColor(R.id.tv_contact_name, Tools.getColor(R.color.text_lgray));
                    holder.setTextColor(R.id.tv_goods_info, Tools.getColor(R.color.text_lgray));
                }
                if ("0".equals(batchBean.fee2Id)) {
                    holder.setText(R.id.tv_fee_1, "免门票");
                } else if ("1".equals(batchBean.fee2Id)) {
                    holder.setText(R.id.tv_fee_1, "未收门票");
                } else if (batchBean.fee2Id.startsWith("33")) {
                    holder.setText(R.id.tv_fee_1, "未收门票");
                } else if ("55".equals(batchBean.fee2Id)) {
                    holder.setText(R.id.tv_fee_1, "已收门票");
                } else {
                    holder.setText(R.id.tv_fee_1, "未知门票");
                }
                if ("0".equals(batchBean.fee3Id)) {
                    holder.setText(R.id.tv_fee_2, "免进场费");
                } else if ("1".equals(batchBean.fee3Id)) {
                    holder.setText(R.id.tv_fee_2, "未收进场费");
                } else if (batchBean.fee3Id.startsWith("33")) {
                    holder.setText(R.id.tv_fee_2, "未收进场费");
                } else if ("55".equals(batchBean.fee3Id)) {
                    holder.setText(R.id.tv_fee_2, "已收进场费");
                } else {
                    holder.setText(R.id.tv_fee_2, "未知进场费");
                }
            }
            chile.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(ToOutBatchCarListActivity.this, BatchDetailActivity.class);
                    intent.putExtra("inBatchId", batchBean.inBatchId);
                    intent.putExtra("inCarId", inBatchListBean.incarInfo.inCarId);
                    intent.putExtra("isOutCar", false);
                    startActivity(intent);
                }
            });
            mLlContent.addView(chile);
        }
    }

    @Override
    public void loadInCarListSuccess(NetInBatchListBean inCarList) {
        setData(inCarList);
    }

    @Override
    public void toOutCarSuccess() {
        mTvOutCar.setBackgroundResource(R.color.color_e4e4e4);
        mTvOutCar.setEnabled(false);
        mTvDiffOutCar.setBackgroundResource(R.color.color_e4e4e4);
        mTvDiffOutCar.setEnabled(false);
    }
}
